package com.yang.util;

import java.io.*;

public class AdbConnect {
    private static Runtime runtime = null;
    private static Process process = null;
    private static InputStream inputStream = null;

    public static String run(String cmd) {
        String text = "";
        try {
            if(process!=null&&process.isAlive()){
                run(cmd);
            }else{
                runtime = Runtime.getRuntime();
                process = runtime.exec(cmd);
                inputStream = process.getInputStream();
                byte[] manages = new byte[1024];
                while ((inputStream.read(manages,0,manages.length)) != -1) {
                    text += new String(manages);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (inputStream != null) inputStream.close();
                if (process != null) process.destroyForcibly();
                if (runtime != null) runtime.gc();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.out.println("正在执行命令：" + cmd + "\n"
                + "返回结果：" + text);
        return text;
    }

}
